﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using Zero.DataLayer;
using Zero.POCO;
using Zero.Presenter;

namespace DevExpress.MailClient.Win.Forms.Campagne
{
    public partial class _frmEditAnnonceur : DevExpress.XtraBars.Ribbon.RibbonForm, IEditAnnonceurView
    {
        bool _isAnnonceurModified;
        bool _newAnnonceur = true;
        public Annonceur annonceur { get; set; }
        public EditionAnnonceurPresenter _editionAnnonceurPresenter;
        public Guid AnnonceurId { get; set; }
        public _frmEditAnnonceur(Annonceur annonceurHist,bool newAnnonceur)
        {
            InitializeComponent();
            DialogResult = DialogResult.Cancel;
            CBEDommaine.SelectedIndex = 0;
            _newAnnonceur = newAnnonceur;

            if (_newAnnonceur)
            {
                AnnonceurId = default(Guid);
                BBICancel.Enabled = false;
                BBINext.Enabled = false;
                BBIPrev.Enabled = false;
                BBIDelete.Enabled = false;
            }
            else
            {
                AnnonceurId = annonceurHist.id;
            }




            

            _editionAnnonceurPresenter = new EditionAnnonceurPresenter(this, new AnnonceurRepository(), AnnonceurId);
            _editionAnnonceurPresenter.Display();


            if (!_newAnnonceur)
            {
                TEName.Text = annonceur.Name;
                CBEDommaine.SelectedItem = annonceur.Domaine;
            }

            _isAnnonceurModified = _newAnnonceur ;


            GCMarque.DataSource = annonceur.Marques;
            GVMarque.Columns[2].Visible = false;
        }

        private void BBIAdmarqued_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            var form = new frmEditMarque(null,true);
            //form.Load += OnEditMailFormLoad;
            //form.FormClosed += OnEditMailFormClosed;
            form.Location = new Point(Left + (Width - form.Width) / 2, Top + (Height - form.Height) / 2);
            form.ShowDialog();
            form.Activate();
            var resultMarque = form.marque;
            if(resultMarque!=null)
                annonceur.Marques.Add(resultMarque);

            GCMarque.DataSource = annonceur.Marques;
            GVMarque.PopulateColumns();
            GVMarque.Columns[2].Visible = false;
            Cursor.Current = Cursors.Default;
        }

        /*void OnEditMailFormLoad(object sender, EventArgs e)
        {
            var form = sender as frmEditCampagne;
            if (form != null)
                form.SaveMessage += OnEditMailFormSaveMessage;
        }*/


        void OnEditMailFormSaveMessage(object sender, EventArgs e)
        {
            /*     frmEditCampagne form = sender as frmEditCampagne;
                 if (form == null)
                     return;

                 if (!DataHelper.Messages.Contains(form.SourceCompaign))
                     DataHelper.Messages.Add(form.SourceCompaign);*/
            //RaiseUpdateTreeViewMessages();
        }


       /* void OnEditMailFormClosed(object sender, FormClosedEventArgs e)
        {
            frmEditCampagne form = sender as frmEditCampagne;
            if (form != null)
                form.SaveMessage -= OnEditMailFormSaveMessage;
        }*/


        private void BBIDeleteMarque_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        DialogResult QueryDelete()
        {
            DialogResult result = XtraMessageBox.Show(this, Properties.Resources.DeleteQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
            switch (result)
            {
                case DialogResult.Cancel:
                    return DialogResult.Cancel;
                case DialogResult.No:
                    return DialogResult.No;
                default:
                    DeleteRecord();
                    return DialogResult.Yes;
            }
        }

       
        DialogResult QueryClose()
        {
            if (!_isAnnonceurModified)
                return DialogResult.Yes;

            DialogResult result = XtraMessageBox.Show(this, Properties.Resources.SaveQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
            switch (result)
            {
                case DialogResult.Cancel:
                    return DialogResult.Cancel;
                case DialogResult.No:
                    return DialogResult.No;
                default:
                    ApplyChanges();
                    return DialogResult.Yes;
            }
        }

        

        private void frmEditAnnonceur_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = QueryClose();
            e.Cancel = result == DialogResult.Cancel;
        }

        private void textEdit1_TextChanged(object sender, EventArgs e)
        {
            _isAnnonceurModified = true;
        }

        private void comboBoxEdit1_SelectedValueChanged(object sender, EventArgs e)
        {
            _isAnnonceurModified = true;
        }

        private void GVMarque_RowCountChanged(object sender, EventArgs e)
        {
            _isAnnonceurModified = true;
        }

        private void GVMarque_CellValueChanged(object sender, XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            _isAnnonceurModified = true;
        }

        private void ribbonControl_ItemClick(object sender, ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "BBISave" :
                    {
                        _isAnnonceurModified = false;
                        annonceur.Name = TEName.Text;
                        annonceur.Domaine = CBEDommaine.SelectedText.ToDomaine();
                        _editionAnnonceurPresenter.WriteChanges();

                        break;
                    }
                case "BBISaveClose" :
                    {
                        _isAnnonceurModified = false;
                        DialogResult result = QueryClose();
                        annonceur.Name = TEName.Text;
                        annonceur.Domaine = CBEDommaine.SelectedText.ToDomaine();
                        _editionAnnonceurPresenter.WriteChanges();
                        if(result==DialogResult.Yes) this.Close();
                        break;
                    }
                case "BBIDelete" :
                    {
                        DialogResult result = QueryDelete();
                        if (result == DialogResult.Yes) this.Close();
                        break;
                    }
                case "BBIRefresh":
                    {
                        GCMarque.DataSource = annonceur.Marques;
                        GVMarque.PopulateColumns();
                        GVMarque.Columns[2].Visible = false;
                       
                        break;
                    }
            }
        }

        private void DeleteRecord()
        {
            throw new NotImplementedException();
        }

        private void ApplyChanges()
        {
            throw new NotImplementedException();
        }

        private void textEdit1_Properties_Validating(object sender, CancelEventArgs e)
        {
            if (TEName.Text.Trim().Length <= 0)
                e.Cancel = true;
        }


    
        
    }
}